package com.ethink.platform.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ethink.order.api.PurchaseOrderFacade;
import com.ethink.order.api.enums.PurchaseReturnOrderReturnTypeEnum;
import com.ethink.order.api.info.*;
import com.ethink.order.api.model.PurchaseDetailModel;
import com.ethink.order.api.model.PurchaseReturnDetailModel;
import com.ethink.order.api.model.SaleDetailOrderModel;
import com.ethink.order.api.response.PurchaseOrderDetailQueryResponse;
import com.ethink.order.api.response.PurchaseTransferReturnQueryResponse;
import com.ethink.warehouse.api.LogisticsTraceApplyOrderFacade;
import com.ethink.warehouse.api.LogisticsTraceDetailOrderFacade;
import com.ethink.warehouse.api.MateInventoryFacade;
import com.ethink.warehouse.api.enums.LogisticsTraceDetailOrderTypeEnum;
import com.ethink.warehouse.api.enums.SubjectEnum;
import com.ethink.warehouse.api.info.MateInventoryDetailInfo;
import com.ethink.warehouse.api.request.LogisticsTraceApplyAddRequest;
import com.ethink.warehouse.api.request.MateInventoryLockLogAddRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName PurchaseReturnManager.java
 * @Description
 * @createTime 2022年12月07日 21:03:00
 */
@Component
public class PurchaseReturnManager {

    @Autowired
    private PurchaseOrderFacade purchaseOrderFacade;

    @Autowired
    private MateInventoryFacade mateInventoryFacade;

    @Autowired
    private LogisticsTraceApplyOrderFacade logisticsTraceApplyOrderFacade;


    public PurchaseTransferReturnQueryResponse purchaseTransferQuery(Long id, Integer returnType) {
        PurchaseTransferReturnQueryResponse response = new PurchaseTransferReturnQueryResponse();
        PurchaseOrderDetailQueryResponse purchaseOrderDetailQueryResponse = purchaseOrderFacade.queryPurchaseOrderDetail(id);
        response.setMain(purchaseOrderDetailQueryResponse.getMain());
        List<PurchaseTransferReturnInfo> infos = new ArrayList<>();
        int count = 1;
        if (PurchaseReturnOrderReturnTypeEnum.S1.getIntValue().equals(returnType)) {
            for (PurchaseDetailModel purchaseDetailModel : purchaseOrderDetailQueryResponse.getItem()) {
                PurchaseTransferReturnInfo info = getPurchaseTransferReturnInfo(count, purchaseDetailModel, null);
                count++;
                infos.add(info);
            }
        } else if (PurchaseReturnOrderReturnTypeEnum.S2.getIntValue().equals(returnType)) {
            List<String> detailOrderNo = CollUtil.getFieldValues(purchaseOrderDetailQueryResponse.getItem(), "detailOrderNo", String.class);
            List<MateInventoryDetailInfo> mateInventoryDetailInfos = mateInventoryFacade.listMateInventoryByPurchaseDetailOrderNoList(detailOrderNo);
            Map<String, PurchaseDetailModel> map = new HashMap<>();
            for (PurchaseDetailModel purchaseDetailModel : purchaseOrderDetailQueryResponse.getItem()) {
                map.put(purchaseDetailModel.getDetailOrderNo(), purchaseDetailModel);
            }
            for (MateInventoryDetailInfo mateInventoryDetailInfo : mateInventoryDetailInfos) {
                PurchaseDetailModel purchaseDetailModel = map.get(mateInventoryDetailInfo.getPurchaseDetailOrderNo());
                if (mateInventoryDetailInfo.getMateSku().equals(purchaseDetailModel.getMateSku())) {
                    PurchaseTransferReturnInfo info = getPurchaseTransferReturnInfo(count, purchaseDetailModel, mateInventoryDetailInfo);
                    count++;
                    infos.add(info);
                }
            }
        }
        response.setItem(infos);
        return response;
    }

    private PurchaseTransferReturnInfo getPurchaseTransferReturnInfo(int count, PurchaseDetailModel purchaseDetailModel
            , MateInventoryDetailInfo mateInventoryDetailInfo) {
        PurchaseTransferReturnInfo info = new PurchaseTransferReturnInfo();
        info.setKey(count);
        info.setPurchaseOrderNo(purchaseDetailModel.getOrderNo());
        info.setPurchaseDetailOrderNo(purchaseDetailModel.getDetailOrderNo());
        info.setMateCode(purchaseDetailModel.getMateCode());
        info.setMateName(purchaseDetailModel.getMateName());
        info.setMateSku(purchaseDetailModel.getMateSku());
        info.setMateSkuName(purchaseDetailModel.getMateSkuName());
        info.setCategoryName(purchaseDetailModel.getCategoryName());
        info.setBrandCode(purchaseDetailModel.getBrandCode());
        info.setBrandName(purchaseDetailModel.getBrandName());
        info.setUnitCode(purchaseDetailModel.getUnitCode());
        info.setUnitName(purchaseDetailModel.getUnitName());
        info.setProductSpecification(purchaseDetailModel.getProductSpecification());
        info.setPurchasePrice(purchaseDetailModel.getPurchasePrice());
        info.setLockOrderQty(purchaseDetailModel.getLockOrderQty());
        if (ObjectUtil.isNotEmpty(mateInventoryDetailInfo)) {
            info.setPurchaseCode(mateInventoryDetailInfo.getPurchaseCode());
            info.setInventoryType(mateInventoryDetailInfo.getInventoryType());
            info.setProductDate(mateInventoryDetailInfo.getProductDate());
            info.setCountryOfOrigin(mateInventoryDetailInfo.getCountryOfOrigin());
            info.setProduceCode(mateInventoryDetailInfo.getProduceCode());
            info.setExpireDate(mateInventoryDetailInfo.getExpireDate());
            info.setAvailableQty(mateInventoryDetailInfo.getAvailableQty());
            info.setSubWarehouseCode(mateInventoryDetailInfo.getSubWarehouseCode());
            info.setSubWarehouseName(mateInventoryDetailInfo.getSubWarehouseName());
            info.setMateInventoryId(mateInventoryDetailInfo.getId());
        }
        return info;
    }

    public void lockMateInventory(List<PurchaseReturnDetailModel> returnDetailOrderInfoList) {
        // 销售单提交后，冻结库存，
        List<MateInventoryLockLogAddRequest> requests = new ArrayList<>();
        for (PurchaseReturnDetailOrderInfo detailOrderInfo : returnDetailOrderInfoList) {
            MateInventoryLockLogAddRequest mateInventoryLockLogAddRequest = new MateInventoryLockLogAddRequest();
            mateInventoryLockLogAddRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
            mateInventoryLockLogAddRequest.setSourceOrderNo(detailOrderInfo.getOrderNo());
            mateInventoryLockLogAddRequest.setSourceDetailOrderNo(detailOrderInfo.getDetailOrderNo());
            mateInventoryLockLogAddRequest.setSourceOrderType(SubjectEnum.T12.getCode());
            mateInventoryLockLogAddRequest.setSubject(SubjectEnum.T12.getCode());
            mateInventoryLockLogAddRequest.setMateInventoryId(detailOrderInfo.getMateInventoryId());
            mateInventoryLockLogAddRequest.setLockQty(detailOrderInfo.getLessQty());
            requests.add(mateInventoryLockLogAddRequest);
        }
        mateInventoryFacade.lockMateInventory(requests);
    }

    public void releaseLockMateInventory(List<PurchaseReturnDetailModel> returnDetailOrderInfoList) {
        // 审批拒绝,释放库存
        List<MateInventoryLockLogAddRequest> requests = new ArrayList<>();
        for (PurchaseReturnDetailOrderInfo detailOrderInfo : returnDetailOrderInfoList) {
            MateInventoryLockLogAddRequest mateInventoryLockLogAddRequest = new MateInventoryLockLogAddRequest();
            mateInventoryLockLogAddRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
            mateInventoryLockLogAddRequest.setSourceOrderNo(detailOrderInfo.getOrderNo());
            mateInventoryLockLogAddRequest.setSourceDetailOrderNo(detailOrderInfo.getDetailOrderNo());
            mateInventoryLockLogAddRequest.setSourceOrderType(SubjectEnum.T12.getCode());
            mateInventoryLockLogAddRequest.setSubject(SubjectEnum.T12.getCode());
            mateInventoryLockLogAddRequest.setMateInventoryId(detailOrderInfo.getMateInventoryId());
            mateInventoryLockLogAddRequest.setLockQty(detailOrderInfo.getLessQty());
            requests.add(mateInventoryLockLogAddRequest);
        }
        mateInventoryFacade.releaseLockMateInventory(requests);
    }

    /**
     * 采购退款退货通过后，需要创建出库申请, 同时锁定库存
     */
    public void t12(PurchaseReturnOrderInfo purchaseReturnOrderInfo,
                    List<PurchaseReturnDetailOrderInfo> returnDetailOrderInfoList,
                    PurchaseOrderInfo purchaseOrderInfo) {
        LogisticsTraceApplyAddRequest addRequest = new LogisticsTraceApplyAddRequest();
        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T2.getIntValue());
        addRequest.setSourceOrderNo(purchaseReturnOrderInfo.getOrderNo());
        addRequest.setSourceOrderType(SubjectEnum.T12.getCode());
        addRequest.setSubject(SubjectEnum.T12.getCode());
        addRequest.setCreateUserCode(purchaseReturnOrderInfo.getCreateUserCode());
        addRequest.setCreateUserName(purchaseReturnOrderInfo.getCreateUserName());
        addRequest.setCompanyCode(purchaseOrderInfo.getSupplierCode());
        addRequest.setCompanyName(purchaseOrderInfo.getSupplierName());
        addRequest.setLock(false);
        addRequest.setT12Items(returnDetailOrderInfoList.stream().map(detailInfo -> {
            LogisticsTraceApplyAddRequest.T12Item mateItem = new LogisticsTraceApplyAddRequest.T12Item();
            mateItem.setSourceDetailOrderNo(detailInfo.getDetailOrderNo());
            mateItem.setMateInventoryId(detailInfo.getMateInventoryId());
            mateItem.setApplyInQty(detailInfo.getLessQty());
            return mateItem;
        }).collect(Collectors.toList()));
        logisticsTraceApplyOrderFacade.addLogisticsTraceApplyOrder(addRequest);
    }
}
